package com.example.back.Controller;

import com.example.back.Annotation.UserLoginToken;
import com.example.back.Entity.OrderItem;
import com.example.back.Repository.OrderItemRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 订单模块，已经支付之后的订单
 */
@Slf4j
@UserLoginToken
@RestController
@RequestMapping("/orderItem")
public class OrderItemController {
    final int RETURN_PAGE_SIZE = 10;
    @Autowired
    private OrderItemRepository orderitemRepository;

    //增加新的订单
    @UserLoginToken
    @PostMapping("")
    public boolean addNewOrder(@RequestBody OrderItem orderItem) {
        try {
            //将订单的状态设定为等待发货
            orderItem.setState("等待发货");
            log.info(orderItem.getPrice()+"");
            orderItem.setBuyDate(new Date());
            orderitemRepository.save(orderItem);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    //根据买家ID查询Order中的商品
    @UserLoginToken
    @GetMapping("/buyerList/{bId}/{page}")
    public Page<OrderItem> findByBuyerId(@PathVariable("bId") long bid,
                                         @PathVariable("page") int page) {
        Pageable pageable = PageRequest.of(page - 1, RETURN_PAGE_SIZE, Sort.Direction.ASC, "buyDate");
        return orderitemRepository.findAllByBuyerId(bid, pageable);
    }

    //根据卖家ID查询Order中的商品
    @UserLoginToken
    @GetMapping("/sellerList/{sId}/{page}")
    public Page<OrderItem> findBySellerId(@PathVariable("sId") long sId,
                                         @PathVariable("page") int page) {
        Pageable pageable = PageRequest.of(page - 1, RETURN_PAGE_SIZE, Sort.Direction.ASC, "buyDate");
        return orderitemRepository.findAllBySellerId(sId, pageable);
    }

    //根据订单商品名来查询订单列表
    @UserLoginToken
    @GetMapping("/PNCB/{PNC}/{bId}/{page}")
    public Page<OrderItem> findAllByProductNameContainsAndBuyerId(@PathVariable("PNC") String PNCB,
                                                                  @PathVariable("bId") long bId,
                                                                  @PathVariable("page") int page) {
        Pageable pageable = PageRequest.of(page - 1, RETURN_PAGE_SIZE, Sort.Direction.ASC, "buyDate");
        return orderitemRepository.findAllByProductNameContainsAndBuyerId(PNCB, bId, pageable);
    }

    //根据卖家ID和订单商品名查找订单列表
    @UserLoginToken
    @GetMapping("/PNCS/{PNC}/{bId}/{page}")
    public Page<OrderItem> findAllByProductNameContainsAndSellerId(@PathVariable("PNC") String PNCS,
                                                                   @PathVariable("bId") long sId,
                                                                   @PathVariable("page") int page) {
        Pageable pageable = PageRequest.of(page - 1, RETURN_PAGE_SIZE, Sort.Direction.ASC, "buyDate");
        return orderitemRepository.findAllByProductNameContainsAndBuyerId(PNCS, sId, pageable);
    }

    //根据订单状态查询订单列表
    @UserLoginToken
    @GetMapping("/state/{state}/{sId}/{page}")
    public Page<OrderItem> findAllByState(@PathVariable("state") String state,
                                          @PathVariable("sId") long sId,
                                          @PathVariable("page") int page) {
        Pageable pageable = PageRequest.of(page - 1, RETURN_PAGE_SIZE, Sort.Direction.ASC, "buyDate");
        return orderitemRepository.findAllByStateAndSellerId(pageable, state, sId);
    }

    //更新订单状态
    @UserLoginToken
    @PatchMapping("/state/{oid}/{newState}")
    public void patchOrderState(@PathVariable("oid") long oid,
                                @PathVariable("newState") String newState) {
        orderitemRepository.setNewState(newState, oid);
    }

    //删除订单
    @UserLoginToken
    @DeleteMapping("/{oid}")
    public boolean deleteOrderByOrderId(@PathVariable("oid") long oid) {
        try {
            orderitemRepository.deleteById(oid);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
